/*
 * Copyright (c) 2012 Jonathan Tyers
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.org.uplink.configproxy.validator;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import uk.org.uplink.configproxy.Patterns;

/**
 * Implements the {@link Patterns#AUTO} auto-selection of patterns based on return type.
 * 
 * @author jonny
 */
public class PatternSelector {
	private static final Map<Class<?>, String> PATTERN_MAPPINGS;
	static {
		HashMap<Class<?>, String> mappings = new HashMap<Class<?>, String>();

		mappings.put(BigDecimal.class, Patterns.DECIMAL);
		mappings.put(Double.class, Patterns.DECIMAL);
		mappings.put(Short.class, Patterns.DECIMAL);

		mappings.put(Integer.class, Patterns.INTEGER);
		mappings.put(Long.class, Patterns.INTEGER);

		mappings.put(Boolean.class, Patterns.BOOLEAN);

		PATTERN_MAPPINGS = Collections.unmodifiableMap(mappings);
	}

	/**
	 * Selects a pattern from {@link Patterns} that will validate string representations of <tt>clazz</tt>.
	 * 
	 * @param clazz
	 * @return
	 */
	public String selectPattern(final Class<?> clazz) {
		if (PATTERN_MAPPINGS.containsKey(clazz)) {
			return PATTERN_MAPPINGS.get(clazz);

		} else {
			return Patterns.ANYTHING;
		}
	}
}
